package com.aion.jirc.gui;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;
import java.awt.event.ActionListener;

/**
 * Created by IntelliJ IDEA.
 * Date: 11-01-30
 * Time: 17:44
 *
 * @author Aion
 */
public class Menubar extends JMenuBar {

    private final static String[] TITLES;
    private final static String[][] ELEMENTS;

    private ActionListener listener;

    static {
        TITLES = new String[]{"File", "View", "Tools"};
        ELEMENTS = new String[][]{
                {"Connect", "Disconnect", "-", "Exit"},
                {"CheckT Menubar", "CheckT Toolbar", "CheckT Switchbar", "CheckF Debug"},
                {"CheckF Disable CTCPs"}
        };
    }

    protected Menubar(ActionListener listener) {
        super();
        this.listener = listener;
        for (int i = 0; i < TITLES.length; i++) {
            String title = TITLES[i];
            String[] elements = ELEMENTS[i];
            add(constructMenu(title, elements));
        }
    }

    private JMenu constructMenu(String title, String[] elements) {
        JMenu menu = new JMenu(title);
        for (String element : elements) {
            if (element.equals("-")) {
                menu.add(new JSeparator());
            } else {
                JMenuItem menuItem;
                if (element.startsWith("Check")) {
                    char state = element.charAt(5);
                    element = element.substring(7);
                    menuItem = new JCheckBoxMenuItem(element);
                    menuItem.setSelected(state == 'T');
                } else {
                    menuItem = new JMenuItem(element);
                }
                menuItem.addActionListener(listener);
                menuItem.setActionCommand(title + "." + element);
                menu.add(menuItem);
            }
        }
        return menu;
    }
}
